/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：实现凯撒算法的加密解密功能
* CreateTime：2020-06-23 10:48:35
* Version：1.0
* Modify Recorder：
*************************************************/

using System;
using System.Text;

namespace TinyToolKit
{
    public class EncryptDecrypt_Caesar : EncryptDecryptBase 
    {
        private int offset = 118;

        /// <summary>
        /// 偏移量
        /// </summary>
        public int Offset
        {
            set => offset = value;
        }

        public override string Encrypt(string content)
        {
            byte[] data = Encoding.UTF8.GetBytes(content);
            byte[] encryptData = Encrypt(data);
            return Convert.ToBase64String(encryptData);
        }

        public override string Decrypt(string content)
        {
            byte[] data = Convert.FromBase64String(content);
            byte[] decryptData = Decrypt(data);
            return Encoding.UTF8.GetString(decryptData);
        }

        public override byte[] Encrypt(byte[] content)
        {
            byte[] temp = new byte[content.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = (byte)((content[i] + offset) % 256);
            }
        
            return temp;
        }

        public override byte[] Decrypt(byte[] content)
        {
            byte[] temp = new byte[content.Length];
            int antiOffset = 256 - offset;
            for (int i = 0; i < temp.Length; i++)
            {
                temp[i] = (byte)((content[i] + antiOffset) % 256);
            }

            return temp;
        }
    }

}
